const db = wx.cloud.database();
Page({
  data: {
    note: [],
    video: [],
    active: 0,
    select: 0
  },
  onChange(event) {
    this.setData({
      select: event.detail.name
    })
  },
  onLoad: function () {
    var that = this;
    that.getCount()
    that.getVideoCount()
    that.getList()
    that.getVideoList()
  },
  // 点击查看详细文字
  detail(e) {
    console.log(e.target.dataset.id)
    var that = this;
    db.collection('scenery').where({
        _id: e.target.dataset.id
      })
      .get({
        success: function (res) {
          console.log(res.data[0].intr)
          wx.showModal({
            title: res.data[0].title,
            content: res.data[0].intr,
            showCancel: false
          })
        }
      })
  },
  //获取条数图片
  getCount() {
    var that = this;
    db.collection("scenery").count({
      success: function (res) {
        console.log(res.total)
        that.setData({
          dataSum: res.total
        })
      }
    })
  },
  //获取条数视频
  getVideoCount() {
    var that = this;
    db.collection("video").count({
      success: function (res) {
        console.log(res.total)
        that.setData({
          dataVideoSum: res.total
        })
      }
    })
  },
  //获取八条数据图片
  getList() {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    db.collection('scenery').limit(6).get({
      success: function (res) {
        that.setData({
          note: res.data,
        })
        that.data.note = res.data;
        wx.hideLoading({
          success: (res) => {},
        })
      },
      fail: function (event) {}
    })
  },
  //获取八条数据视频
  getVideoList() {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    db.collection('video').limit(4).get({
      success: function (res) {
        that.setData({
          video: res.data,
        })
        that.data.video = res.data;
        wx.hideLoading({
          success: (res) => {},
        })
      },
      fail: function (event) {}
    })
  },
  /* 
   * 页面上拉触底事件的处理函数图片
   * 
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.select == 1) {
      console.log(1)
      var temp = [];
      if (that.data.video.length < that.data.dataVideoSum) {
        try {
          wx.showLoading({
            title: '加载中...',
          })
          db.collection('video').skip(that.data.video.length).limit(4).orderBy('time', 'desc').get({

            success: function (res) {

              if (res.data.length > 0) {
                for (var i = 0; i < res.data.length; i++) {
                  var tempshopping = res.data[i];
                  temp.push(tempshopping);
                }
                var dataVideoSum = {};
                dataVideoSum = that.data.video.concat(temp);
                that.setData({
                  video: dataVideoSum,
                })
              }
              wx.hideLoading();
            },
            fail: function (event) {
              console.log("======" + event);
            }
          })
        } catch (e) {
          console.error(e);
        }
      } else if (that.data.video.length >= that.data.dataVideoSum) {
        wx.showToast({
          title: '已为你加载全部',
          icon: 'none'
        })
      }
    } else if(that.data.select==0) {
      console.log(2)
      var temp1 = [];
      if (that.data.note.length < that.data.dataSum) {
        try {
          wx.showLoading({
            title: '加载中...',
          })
          db.collection('scenery').skip(that.data.note.length).limit(6).get({
            success: function (res) {
              if (res.data.length > 0) {
                for (var i = 0; i < res.data.length; i++) {
                  var tempshopping1 = res.data[i];
                  temp1.push(tempshopping1);
                }
                var dataNote = {};
                dataNote = that.data.note.concat(temp1);
                that.setData({
                  note: dataNote,
                })
              }
              wx.hideLoading();
            },
            fail: function (event) {
              console.log("======" + event);
            }
          })
        } catch (e) {
          console.error(e);
        }
      } else if (that.data.note.length >= that.data.dataSum) {
        wx.showToast({
          title: '已为你加载全部',
          icon: 'none'
        })
      }
    }
  },
  onPullDownRefresh: function () {
    var that = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    setTimeout(function () {
      that.getList()
      that.getVideoList()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1000);

  },
  ImagePreview(e) {
    console.log(e)
    var that = this;
    var src = e.target.dataset.url; //获取data-src
    var imgList = [e.target.dataset.url]; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  }
})